import { OBJECT_COPY } from '../../../Core/CoreDefine';
import GameMgr from '../../Logic/GameMgr';
import { JXBtlCamp, JXBtlPs } from './JXBattleDefine';
import JXBtlMgr from './JXBattleManager';
import { JXBattleEntity, JXBattleHero, JXBattleTeamAmbienter, JXBattleTeamEntiry } from './JXBattleRole';
import { JXBattleMap } from './JXBattleUtility';
import JXRBCmdMgr from './JXRBCmdMgr';


export default class JXBattleTeam {

    /** 参战角色信息 */
    public roles: JXBattleMap<number, JXBattleTeamEntiry>;
    /** 脱战角色信息 */
    public outBtlRoles: JXBattleMap<number, JXBattleTeamEntiry>;
    /** 环境对象 */
    public ambienter: JXBattleTeamAmbienter;

    public iTeam: IMonsterBtl = null;

    public size: number = 100;

    /** 阵营 */
    public camp: number;
    public cmd: JXRBCmdMgr;
    constructor(iTeam: IMonsterBtl, camp: number, cmd: JXRBCmdMgr) {
        this.camp = camp;
        this.cmd = cmd;
        this.iTeam = iTeam;
        this.roles = new JXBattleMap<number, JXBattleTeamEntiry>();
        this.outBtlRoles = new JXBattleMap<number, JXBattleTeamEntiry>();
        let skills: number[][] = [];
        // 添加环境对象
        this.ambienter = new JXBattleTeamAmbienter(skills, this.camp + 1, this);
    }

    /**上阵 */
    public goBattle(heroId: string) {
        let hero = null;
        let card: IHeroBtlData = null;
        for (let i = 0; i < this.iTeam.partnerData.length; i++) {
            if (this.iTeam.partnerData[i].heroId === heroId) {
                card = OBJECT_COPY<IHeroBtlData>(this.iTeam.partnerData[i]);
            }
        }
        if (!card) return;
        if (this.camp === JXBtlCamp.Defenser) {
            card.heroId = GameMgr.rHeroData.insIdGentor.getNewId();
        }
        if (card.curHp == 0) return; // 存在并且为0表示死亡
        hero = new JXBattleHero(card, this, this.size);
        card.index = this.size;
        this.roles.set(this.size, hero);
        this.size++;
        return card;
    }

    public outBattle(id: number) {
        if (this.roles.has(id)) {
            let role = this.roles.get(id);
            this.outBtlRoles.set(id, role)
            this.roles.delete(id)
        }
    }

    public goBtlById(id: number) {
        if (this.outBtlRoles.has(id)) {
            let role = this.outBtlRoles.get(id);
            this.roles.set(id, role);
            this.outBtlRoles.delete(id)
        } else {
            cc.error("上周失败")
        }
    }

    /**合成 */
    public sycRole(id: number, needId: number) {
        let role = this.outBtlRoles.get(id);
        let sycRole = this.outBtlRoles.get(needId);
        if (!role || !sycRole) {
            cc.error("合成失败,");
            return;
        }
        role.maxHp += sycRole.maxHp;
        role.curHp += sycRole.curHp;
        role.lv += 1;
        role.propts.forEach((v, i) => {
            if (i < JXBtlPs.R.PORPTS.MAXHP) {
                role.propts[i] += sycRole.propts[i]
            }
        })
        sycRole.onDie()
        this.outBtlRoles.delete(needId)
        return { maxHp: role.maxHp, curHp: role.curHp }
    }

    /** 获取敌人 */
    public enemyTeam(): JXBattleTeam {
        return JXBtlMgr.ins.getTeamByCamp(this.camp > 0 ? 0 : 1);
    }

    /** 获取角色数据 */
    public getRoles(condition?: { (role: JXBattleEntity): boolean }): JXBattleTeamEntiry[] {
        let ret = [];
        this.roles.forEach(v => {
            if (!condition || condition(v)) {
                ret.push(v);
            }
        });
        return ret;
    }

    /**根据节点跟id 获取未死亡的玩家数据*/
    public getRolesByPos(pos: cc.Vec2, scopeId: number): JXBattleTeamEntiry[] {
        let ret: JXBattleTeamEntiry[] = []
        let adj = GameMgr.mapMgr.getAdjacent(scopeId, pos);
        this.roles.forEach((v: JXBattleTeamEntiry, k: number) => {
            let role = this.cmd.getRoleById(v.id);
            if (cc.isValid(role)) {
                let pos = cc.v2()
                pos = this.cmd.btlMapConverts.converToMapPos(role.birthPos);
                for (let i = 0; i < adj.length; i++) {
                    let target = adj[i];
                    if (target.x === pos.x && target.y === pos.y && !v.isDie && v.jxRole.checkRoleIsEnd()) {
                        ret.push(v)
                        break;
                    }
                }
            }
        });
        return ret;
    }

}